package com.jacpy.sb;

import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.jacpy.busline.widget.BusLineView;
import com.jacpy.busline.widget.model.BusLineItem;
import com.jacpy.sb.model.StationInfo;
import com.jacpy.sb.utils.Netdataanalysis;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements BusLineView.OnBusStationClickListener {

    public String result_station;
    List<StationInfo> stationlist = new ArrayList<>();
    BusLineView view;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        view = (BusLineView) findViewById(R.id.bus_line);

        view.setOnBusStationClickListener(this); // 设置公交站点的点击事件
        new MyThread().start();
        handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                switch (msg.what) {
                    case 0:
                        // Log.d("may", "result_station: " + result_station);
                        analyseStationName(result_station);
                        view.setBusLineData(initData1()); // 设置数据
                        break;
                }
            }
        };


    }

    public List<StationInfo> analyseStationName(String resStr) {
        resStr = resStr.trim();
        String[] data = resStr.split("\n");
        for (int i = 0; i < data.length; i++) {
            StationInfo info = new StationInfo();
            String datainfo = data[i].trim();
            String[] dataitems = datainfo.split(" ");
            info.setStationName(dataitems[0]);
            stationlist.add(info);
        }
        return stationlist;
    }

    @Override
    public void onBusStationClick(View view, BusLineItem item) {
        if (BuildConfig.DEBUG) {
            //  Log.d("may", "name: " + item.name);
        }
    }

    private List<BusLineItem> initData1() {
        ArrayList<BusLineItem> list = new ArrayList<>();
        for (int i = 0; i < stationlist.size(); i++) {
            BusLineItem item = new BusLineItem();
            item.name = stationlist.get(i).getStationName();
            if (i==6||i==10||i==13){
                item.isRailwayStation=true;
            }
            if(i==7){
                item.isCurrentPosition=true;//设置当前到达哪个站
                item.busPosition = 0;//设置公交车离当前站点距离，为0的话，公交车用大图标表示
            }
            list.add(item);
        }
        return list;
    }

    private List<BusLineItem> initData() {
        ArrayList<BusLineItem> list = new ArrayList<>();
        BusLineItem item = new BusLineItem();
        item.name = "紫薇阁";
        item.trafficState = initRoadState();
        list.add(item);

        item = new BusLineItem();
        item.name = "景蜜村";
        item.busPosition = 0;
        item.trafficState = initRoadState();
        list.add(item);

        item = new BusLineItem();
        item.name = "福田外语学校东";
        item.trafficState = initRoadState();
        item.isRailwayStation = true;
        item.isCurrentPosition = true;
        list.add(item);

        item = new BusLineItem();
        item.name = "景田沃尔玛";
        item.trafficState = initRoadState();
        item.isRailwayStation = true;
        item.busPosition = 30;
        list.add(item);

        item = new BusLineItem();
        item.name = "特发小区";
        item.trafficState = initRoadState();
        list.add(item);

        item = new BusLineItem();
        item.name = "枫丹雅苑";
        item.isRailwayStation = false;
        list.add(item);

        item = new BusLineItem();
        item.name = "市委党校";
        item.trafficState = initRoadState();
        list.add(item);

        item = new BusLineItem();
        item.name = "香蜜红荔立交";
        item.busPosition = 50;
        list.add(item);

        item = new BusLineItem();
        item.name = "景蜜村";
        item.trafficState = initRoadState();
        list.add(item);

        item = new BusLineItem();
        item.name = "天安数码城";
        item.trafficState = initRoadState();
        list.add(item);

        item = new BusLineItem();
        item.name = "上沙村";
        list.add(item);

        item = new BusLineItem();
        item.name = "沙嘴路口";
        item.trafficState = initRoadState();
        list.add(item);

        item = new BusLineItem();
        item.name = "新洲中学";
        item.busPosition = 80;
        list.add(item);

        item = new BusLineItem();
        item.name = "新洲三街";
        item.trafficState = initRoadState();
        list.add(item);

        item = new BusLineItem();
        item.name = "新洲村";
        list.add(item);

        item = new BusLineItem();
        item.name = "众孚小学";
        item.trafficState = initRoadState();
        list.add(item);

        item = new BusLineItem();
        item.name = "石厦学校";
        list.add(item);

        item = new BusLineItem();
        item.name = "福田区委";
        item.trafficState = initRoadState();
        item.isRailwayStation = true;
        list.add(item);

        item = new BusLineItem();
        item.name = "皇岗村";
        item.busPosition = 50;
        item.isRailwayStation = true;
        list.add(item);

        item = new BusLineItem();
        item.name = "福田保健院";
        item.trafficState = initRoadState();
        item.isRailwayStation = true;
        list.add(item);

        item = new BusLineItem();
        item.name = "福民地铁站";
        item.isRailwayStation = true;
        list.add(item);

        item = new BusLineItem();
        item.name = "福强路口";
        item.trafficState = initRoadState();
        item.busPosition = 80;
        list.add(item);

        item = new BusLineItem();
        item.name = "福田口岸总站";
        item.isRailwayStation = true;
        list.add(item);

        return list;
    }

    private BusLineItem.RoadState[] initRoadState() {
        BusLineItem.RoadState[] rss = new BusLineItem.RoadState[5];
        BusLineItem.RoadState rs = new BusLineItem.RoadState();
        rs.idx = 0;
        rs.start = 0;
        rs.ratio = 0.1f;
        rs.state = BusLineItem.RoadState.ROAD_NORMAL;
        rss[0] = rs;

        rs = new BusLineItem.RoadState();
        rs.idx = 1;
        rs.start = 0.1f;
        rs.ratio = 0.2f;
        rs.state = BusLineItem.RoadState.ROAD_BUSY;
        rss[1] = rs;

        rs = new BusLineItem.RoadState();
        rs.idx = 2;
        rs.start = 0.3f;
        rs.ratio = 0.2f;
        rs.state = BusLineItem.RoadState.ROAD_BLOCK;
        rss[2] = rs;

        rs = new BusLineItem.RoadState();
        rs.idx = 3;
        rs.start = 0.5f;
        rs.ratio = 0.3f;
        rs.state = BusLineItem.RoadState.ROAD_BUSY;
        rss[3] = rs;

        rs = new BusLineItem.RoadState();
        rs.idx = 4;
        rs.start = 0.8f;
        rs.ratio = 0.2f;
        rs.state = BusLineItem.RoadState.ROAD_NORMAL;
        rss[4] = rs;
        return rss;
    }

    public class MyThread extends Thread {

        public void run() {
            //getHttpPost("dd");
            //String urls = "http://192.168.1.250:10085/Buslvzhou/station/station1.txt";
            String server = "http://192.168.1.250:10085/Buslvzhou/";
            String path = "station/station1.txt";
            result_station = httpGet(server, path, null, null);
            handler.sendEmptyMessage(0);

        }
    }


    public String httpGet(String server, String path, String params, String cookie) {
        String jsons = null;
        String httpUrl = server + path + "?" + params;
        System.out.println("****httpGet = " + httpUrl + "　param = " + params);
//		 System.out.println(httpUrl);
        URL url;
        try {
            url = new URL(httpUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setRequestMethod("GET");
            if (cookie != null)
                conn.setRequestProperty("Cookie", cookie);
            conn.setRequestProperty("Content-Language", "zh-cn");
            conn.setRequestProperty("Connection", "keep-alive");
            InputStream input = conn.getInputStream();
            InputStreamReader inputreader = new InputStreamReader(input);
            BufferedReader reader = new BufferedReader(inputreader);
            String str = null;
            StringBuffer sb = new StringBuffer();
            while ((str = reader.readLine()) != null) {
                sb.append(str).append("\n");
            }
            if (sb != null) {
                jsons = sb.toString();
            }
            reader.close();
            inputreader.close();
            input.close();
            conn.disconnect();
            input = null;
            inputreader = null;
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            Log.e("busappmas", "http get error URLException");
            return null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.e("busappmas", "http get error IOException");
            return null;
        } catch (Exception e) {
            Log.e("busappmas", "http get error Exception");
            return null;
        }
        return jsons;
    }

}
